from random import randint
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
driver.get('http://localhost/zcb/index.asp')

isp_list=driver.find_elements_by_tag_name('input')
isp_list[0].send_keys('admin')
isp_list[1].send_keys('admin')
isp_list[2].click()

sleep(2)
driver.switch_to.frame('left')
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()

#删除所有单位
sleep(2)
danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
driver.find_element_by_css_selector(danwei_css).click()

sleep(2)
driver.switch_to.default_content()
driver.switch_to.frame('right')

sleep(1)
driver.find_element_by_id('chkall').click()
sleep(1)
sc = 'input.button:nth-child(2)'
driver.find_element_by_css_selector(sc).click()
sleep(1)
driver.switch_to.alert.accept()
sleep(2)

# 增加一条单位记录
bt_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()

sleep(2)
driver.find_element_by_name('danwei').send_keys('吨')
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()

#增加一个大类
sleep(2)
driver.switch_to.default_content()
driver.switch_to.frame('left')

sleep(2)
lei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
driver.find_element_by_css_selector(lei_css).click()

sleep(1)
driver.switch_to.default_content()
driver.switch_to.frame('right')

sleep(1)
addlei_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(addlei_css).click()

sleep(1)
driver.find_element_by_name('bigclass').send_keys('服装')
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()

#新增一个小类（随机选取一个大类）
sleep(1)
driver.switch_to.default_content()
driver.switch_to.frame('left')

sleep(2)
xlei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
driver.find_element_by_css_selector(xlei_css).click()

sleep(1)
driver.switch_to.default_content()
driver.switch_to.frame('right')

sleep(1)
addxlei_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(addxlei_css).click()

sleep(1)
sel_lst = Select(driver.find_element_by_name('bigclass'))
lst_count = len(sel_lst.options)
rand = randint(0, lst_count-1)
sel_lst.select_by_index(rand)

sleep(1)
driver.find_element_by_name('smallclass').send_keys('添加')
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()

#退出
driver.switch_to.default_content()
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)
# 关闭浏览器
driver.quit()